package com.qf.czh.controller;

import com.github.pagehelper.PageInfo;
import com.qf.czh.mapper.UserMapper;
import com.qf.czh.model.RespBean;
import com.qf.czh.model.RespPageBean;
import com.qf.czh.service.LeaveService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

@Controller
public class LeaveController {
    @Autowired
    LeaveService leaveService;
    RespBean respBean;


    @GetMapping("/leave/page")
    @RequiresPermissions(value ={"user:addLeave" ,"admin:all"}  ,logical = Logical.OR)
    public String leave(){
        return "qingjia/qingjiaList";
    }

    @GetMapping("/leave/data")
    @ResponseBody
    public RespPageBean leaveData(String page, String rows, String sort , String order, String startTime, String endTime, String number , String status){
        int p = Integer.parseInt(page);
        int r = Integer.parseInt(rows);
        PageInfo pageInfo =  leaveService.getLeaveByPage(p, r, sort, order,startTime , endTime,number , status );
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(pageInfo.getTotal());
        respPageBean.setRows(pageInfo.getList());
        return respPageBean;
    }

    @PostMapping("/leave/add")
    @ResponseBody
    public RespBean addLeave(String vacationtype , String startTime , String endTime , String reason){
        Integer integer = leaveService.addLeave(vacationtype, startTime, endTime, reason);
        if (integer == 1){
            respBean = RespBean.ok("添加成功");
        }else {
            respBean = RespBean.error("添加失败");
        }
        return respBean;
    }

    @PostMapping("/leave/update")
    @ResponseBody
    public RespBean editLeave(Integer id ,String vacationtype , String startTime , String endTime , String reason){
        Integer integer = leaveService.updateLeave(id , vacationtype,startTime,endTime,reason);
        if (integer == 1){
            respBean = RespBean.ok("更新成功");
        }else {
            respBean = RespBean.error("更新失败");
        }
        return respBean;
    }

    @PostMapping("/leave/delete")
    @ResponseBody
    public RespBean deleteLeave(@RequestParam("ids[]") String[] ids){
        Integer result = leaveService.deleteLeave(ids);
        if (result == 1){
            respBean = RespBean.ok("删除成功");
        }else {
            respBean = RespBean.error("删除失败");
        }
        return respBean;
    }
}
